const eslint = require('@eslint/js');
const { defineConfig } = require('eslint/config');
const tsPlugin = require('@typescript-eslint/eslint-plugin');
const tsParser = require('@typescript-eslint/parser');
const globals = require('globals');
const prettierPlugin = require('eslint-plugin-prettier');

const isProduction = process.argv.includes('--env=production');

module.exports = defineConfig([
  // 1. 环境配置层
  {
    name: 'env-config',
    files: ['**/*.{js,ts,vue}'],
    ignores: ['**/*.d.ts', '**/node_modules/**', '**/dist/**', '**/public/**'],
    languageOptions: {
      globals: {
        AMap: 'readonly',
        AMapUI: 'readonly',
        ...globals.node,
      },
      ecmaVersion: 2020,
    },
  },

  // 2. ESLint 推荐规则
  {
    extends: [eslint.configs.recommended],
    rules: {
      'no-console': isProduction ? 'warn' : 'off',
      'no-debugger': isProduction ? 'error' : 'off',
      'no-empty': 'off',
      'no-undef': 'off',
      'no-unused-vars': 'warn',
      'require-atomic-updates': 'off',
    },
  },
  // 3. TypeScript 配置
  {
    files: ['src/**/*.ts'],
    ignores: ['**/*.d.ts'],
    plugins: {
      '@typescript-eslint': tsPlugin,
    },
    languageOptions: {
      parser: tsParser,
      parserOptions: { project: './tsconfig.json' },
    },
    rules: {
      ...tsPlugin.configs.recommended.rules,
      // ...tsPlugin.configs.recommendedRequiringTypeChecking.rules,
      '@typescript-eslint/no-inferrable-types': 'off',
      '@typescript-eslint/explicit-module-boundary-types': 'off',
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-empty-function': 'off',
      '@typescript-eslint/no-unused-vars': 'warn',
      '@typescript-eslint/no-this-alias': 'off',
      '@typescript-eslint/no-empty-interface': 'off',
      '@typescript-eslint/triple-slash-reference': 'off',
      '@typescript-eslint/no-unsafe-function-type': 'off',
      '@typescript-eslint/no-unused-expressions': 'off',
    },
  },

  // 4. Prettier 集成
  {
    files: ['**/*.{js,ts}'],
    ignores: ['**/*.d.ts'],
    plugins: { prettier: prettierPlugin },
    rules: { 'prettier/prettier': 'warn' },
  },
]);
